package com.xyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xyy.entity.Patient;
import com.xyy.mapper.PatientMapper;
import com.xyy.service.PatientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 就诊人Service实现类
 */
@Slf4j
@Service
public class PatientServiceImpl implements PatientService {
    
    @Resource
    private PatientMapper patientMapper;
    
    @Override
    public Patient addPatient(Patient createDTO, Long userId) {
        // 检查身份证号是否已存在
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", createDTO.getIdCard());
        Patient existingPatient = patientMapper.selectOne(queryWrapper);
        if (existingPatient == null) {
            // 创建新的就诊人
            Patient patient = new Patient();
            BeanUtils.copyProperties(createDTO, patient);
            patient.setUserId(userId);
            patient.setCreatedAt(LocalDateTime.now());
            
            patientMapper.insert(patient);
            return patient;
        } else {
            throw new RuntimeException("该身份证号已存在");
        }
    }
    
    @Override
    public boolean deletePatient(Long patientId, Long userId) {
        // 检查就诊人是否存在且属于当前用户
        Patient patient = getPatientById(patientId, userId);
        if (patient == null) {
            return false;
        }
        
        return patientMapper.deleteById(patientId) > 0;
    }
    
    @Override
    public Patient updatePatient(Patient patient, Long userId) {
        // 检查就诊人是否存在且属于当前用户
        Patient existingPatient = getPatientById(patient.getId(), userId);
        if (existingPatient == null) {
            throw new RuntimeException("就诊人不存在或无权限修改");
        }
        
        // 如果修改了身份证号，检查是否与其他就诊人重复
        if (patient.getIdCard() != null && !patient.getIdCard().equals(existingPatient.getIdCard())) {
            QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id_card", patient.getIdCard());
            queryWrapper.ne("id", patient.getId());
            Patient duplicatePatient = patientMapper.selectOne(queryWrapper);
            if (duplicatePatient != null) {
                throw new RuntimeException("该身份证号已存在");
            }
        }
        
        patientMapper.updateById(patient);
        return getPatientById(patient.getId(), userId);
    }
    
    @Override
    public List<Patient> getPatientList(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.orderByDesc("created_at");
        
        return patientMapper.selectList(queryWrapper);
    }
    
    @Override
    public Patient getPatientById(Long patientId, Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", patientId);
        queryWrapper.eq("user_id", userId);
        
        return patientMapper.selectOne(queryWrapper);
    }
} 